#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "GLWidget.h"
#include <WalkaWare/WalkaWare.h>

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow           (parent)
	, ui                    (0)
	, gl                    (0)
	, kinectService         (0)
	, pointCloudService     (0)
	, planeDetectionService (0)
{
	this->ui = new Ui::MainWindow();
	this->ui->setupUi(this);

	this->gl = new GLWidget(this);
	this->setCentralWidget(this->gl);

	this->kinectService = new KinectService();
	this->kinectService->setDeviceIndex(0);

	this->pointCloudService = new PointCloudService();
	this->pointCloudService->loadCalibration("C:/tmp/kinect_calibration.yml");

	this->planeDetectionService = new PlaneDetectionService();

	QObject::connect
	(
		this->kinectService,
		SIGNAL(depthReady(WalkaWare::Shared::RawDepthMap,quint32)),
		this->pointCloudService,
		SLOT(extract(WalkaWare::Shared::RawDepthMap,quint32))
	);

	QObject::connect
	(
		this->kinectService,
		SIGNAL(accelerationReady(float,float,float,quint32)),
		this->gl,
		SLOT(setAcceleration(float,float,float,quint32))
	);

	QObject::connect
	(
		this->pointCloudService,
		SIGNAL(pointCloudReady(WalkaWare::Shared::PointCloud,quint32)),
		this->gl,
		SLOT(setPointCloud(WalkaWare::Shared::PointCloud,quint32))
	);

	QObject::connect
	(
		this->pointCloudService,
		SIGNAL(pointCloudReady(WalkaWare::Shared::PointCloud,quint32)),
		this->planeDetectionService,
		SLOT(detect(WalkaWare::Shared::PointCloud,quint32))
	);

	QObject::connect
	(
		this->kinectService,
		SIGNAL(accelerationReady(float,float,float,quint32)),
		this->planeDetectionService,
		SLOT(setAcceleration(float,float,float,quint32))
	);

	QObject::connect
	(
		this->planeDetectionService,
		SIGNAL(planeReady(float,float,float,float,quint32)),
		this->gl,
		SLOT(setPlane(float,float,float,float,quint32))
	);

	this->planeDetectionService->start();
	this->pointCloudService->start();
	this->kinectService->start();
}

MainWindow::~MainWindow(void)
{
	delete this->kinectService;
	delete this->pointCloudService;
	delete this->planeDetectionService;

	delete this->ui;
}
